package com.jgp.common.converter;

import javax.persistence.AttributeConverter;
import javax.persistence.Converter;
import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.util.Objects;

/**
 * 项目   jgp-cloud-parent
 * 作者   loufei
 * 时间   2019/1/4
 */
@Converter(autoApply = true)
public class LocalDateTimeAttributeConverter implements AttributeConverter<LocalDateTime,Timestamp>{
    
    @Override
    public Timestamp convertToDatabaseColumn(LocalDateTime attribute) {
        if(Objects.nonNull(attribute)) {
            return Timestamp.valueOf(attribute);
        }
        return null;
    }
    
    @Override
    public LocalDateTime convertToEntityAttribute(Timestamp dbData) {
        if(Objects.nonNull(dbData)){
            return dbData.toLocalDateTime();
        }
        return null;
    }
}
